package weaver.interfaces.xinri.duss.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.xinri.duss.utils.InvokeHelper;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-03-21
 * @Description: 合同文档流程调用汇联易外部写入合同接口
 * @ModifyLog:
 **/
public class WeaContractCreateAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String remark = requestInfo.getRequestManager().getRemark();
        String fullDate = DateUtil.getFullDate();
        String src = requestInfo.getRequestManager().getSrc();
        String clientIp = requestInfo.getRequestManager().getIp();
        writeLog( ">>>>>WeaContractCreateAction>>>>>requestid=" + requestid );
        try {
            JSONObject paramjson = new JSONObject();
            rs.executeQuery( "select * from " + tablename + " where requestid=?", requestid );
            rs.next();
            String mainid = Util.null2String( rs.getString( "id" ) );
            String htid = Util.null2String( rs.getString( "htid" ) );
            String htbh = Util.null2String( rs.getString( "htbh" ) );
            String ngrgh = Util.null2String( rs.getString( "ngrgh" ) );
            String htje = Util.null2String( rs.getString( "htje" ) );
            String gscode = Util.null2String( rs.getString( "gscode" ) );
            String htmc = Util.null2String( rs.getString( "htmc" ) );
            String bssj = Util.null2String( rs.getString( "a5" ) );
            String xdfcode = Util.null2String( rs.getString( "xdfcode" ) );
            String bm = Util.null2String( rs.getString( "bm" ) );
            String kssj = Util.null2String( rs.getString( "kssj" ) );
            String jssj = Util.null2String( rs.getString( "jssj" ) );
            String htbdw = Util.null2String( rs.getString( "htbdw" ) );
            String htqdmd = Util.null2String( rs.getString( "htqdmd" ) );
            String tjsj = Util.null2String( rs.getString( "tjsj" ) );
            String gdsj = Util.null2String( rs.getString( "gdsj" ) );
            writeLog( ">>>>>WeaContractCreateAction>>>>>htje=" + htje );
            JSONObject contractHead = new JSONObject();
            contractHead.put( "contractNumber", htid );
            contractHead.put( "originContractNumber", htbh );
            contractHead.put( "setOfBooksCode", "DEFAULT_SOB" );
            contractHead.put( "applicantEmployeeId", ngrgh );
            if (Util.getDoubleValue(htje) > 0) {
                contractHead.put( "contractCategory", "CONTRACT" );
                contractHead.put( "contractTypeCode", "CON001" );
            } else {
                contractHead.put( "contractCategory", "FRAMEWORK_AGREEMENT" );
                contractHead.put( "contractTypeCode", "CON002" );
            }
            contractHead.put( "companyCode", gscode );
            contractHead.put( "contractName", htmc );
            contractHead.put( "signDate", bssj );
            contractHead.put( "partnerCategory", "VENDOR" );
            contractHead.put( "partnerCode", xdfcode );
            contractHead.put( "amount", htje );
            contractHead.put( "currencyCode", "CNY" );
            contractHead.put( "remark", InvokeHelper.removeHTMLTags( Util.null2String( remark ) ) );
            contractHead.put( "liableCustDeptNum", bm );
            contractHead.put( "liablePersonEmployeeId", ngrgh );
            contractHead.put( "startDate", kssj );
            contractHead.put( "endDate", jssj );
            contractHead.put( "status", null );
            JSONArray customFormValues = new JSONArray();
            JSONObject customField1 = new JSONObject();
            customField1.put( "fieldCode", "BDW" );
            customField1.put( "value", htbdw );
            customFormValues.add( customField1 );
            JSONObject customField2 = new JSONObject();
            customField2.put( "fieldCode", "QDMD" );
            customField2.put( "value", htqdmd );
            customFormValues.add( customField2 );
            contractHead.put( "customFormValues", customFormValues );
            contractHead.put( "createdDate", bssj );
            contractHead.put( "submittedDate", tjsj );
            contractHead.put( "lastApprovalDate", gdsj );
            paramjson.put( "contractHead", contractHead );
            JSONArray contractLines = new JSONArray();
            rs.executeQuery( "select * from " + tablename + "_dt6 where mainid=?", mainid );
            while (rs.next()) {
                JSONObject contractLine = new JSONObject();
                String fkjdbm = Util.null2String( rs.getString( "fkjdbm" ) );
                String hthje = Util.null2String( rs.getString( "hthje" ) );
                String yzfje = Util.null2String( rs.getString( "yzfje" ) );
                String fktj = Util.null2String( rs.getString( "fktj" ) );
                String yjfkrq = Util.null2String( rs.getString( "yjfkrq" ) );
                contractLine.put( "stageCode", fkjdbm );
                contractLine.put( "amount", hthje );
                contractLine.put( "partnerCode", xdfcode );
                contractLine.put( "dueDate", yjfkrq );
                //contractLine.put("stagePaidPaymentAmount", yzfje);
                contractLine.put( "stagePaidAdvancesAmount", 0 );
                contractLine.put( "partnerBankAccountNo", "" );
                contractLine.put( "paymentConditions", fktj );
                contractLines.add( contractLine );
            }
            paramjson.put( "contractLines", contractLines );
            writeLog( ">>>>>WeaContractCreateAction>>>>>paramjson=" + paramjson );
            String resultStr = InvokeHelper.contract( paramjson.toJSONString() );
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put( "jkqqcs", paramjson.toJSONString() );
            dataMap.put( "jkxyfh", resultStr );
            dataMap.put( "khdip", clientIp );
            dataMap.put( "gllc", requestid );
            dataMap.put( "oacz", src );
            dataMap.put( "jkdysj", fullDate );
            dataMap.put( "djh", htbh );
            dataMap.put( "rqsj", DateUtil.getDateTime() );
            dataMap.put( "jkmc", "4" );
            dataMap.put( "lcid", requestid );
            InvokeHelper.insertApiLog( dataMap, "Contract" );
            JSONObject resultJson = JSONObject.parseObject( resultStr );
            writeLog( ">>>>>WeaContractCreateAction>>>>>resultJson=" + resultJson );
            String message = Util.null2String( resultJson.getString( "message" ) );
            if ("operation completed".equals( message )) {
                return Action.SUCCESS;
            } else {
                requestInfo.getRequestManager().setMessageid( "111111" );
                requestInfo.getRequestManager().setMessagecontent( "汇联易接口返回异常:" + message );
                return Action.FAILURE_AND_CONTINUE;
            }
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}
